package com.own.business.level.controller.user;

import com.own.business.level.business.service.LevelUserService;
import com.own.business.level.entity.po.LevelUser;
import com.own.business.level.entity.vo.LevelUserVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserLevelUserController
 *
 * @author chenxueli
 * @date 2022-07-25 11:48:00
 */
@Tag(name = "用户等级" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@LogOperationModuleAop("用户等级" + ConstantBeanSuffix.USER_CLIENT)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.USER_CLIENT + "level/user")
public class UserLevelUserController extends BaseController {

    @Resource
    public LevelUserService levelUserService;

    @LogOperationAop
    @Operation(summary = "根据用户id获取等级信息")
    @GetMapping("/userId/{userId}")
    public ResultModel<LevelUserVo> getByUserId(
            @Parameter(description = "用户id", required = true) @PathVariable Long userId
    ) {
        var vo = levelUserService.getById(LevelUser::getFkUserId, userId, () -> levelUserService.buildByUserId(userId));
        return R.success(vo);
    }

    @LogOperationAop
    @Operation(summary = "获取当前登录用户的等级信息")
    @GetMapping("/userId")
    public ResultModel<LevelUserVo> getBySelf() {
        var loginUser = SessionUserUtil.getLoginUser();
        loginUser.checkLogin();
        return getByUserId(loginUser.userId());
    }

}
